package com.zbkj.crmeb.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.crmeb.core.utils.RedisUtil;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.member.dao.MerchantMemberTypeDao;
import com.zbkj.crmeb.member.model.ChannelMerchantAttribute;
import com.zbkj.crmeb.member.model.MerchantMemberType;
import com.zbkj.crmeb.member.service.IChannelMerchantAttributeService;
import com.zbkj.crmeb.member.service.IMerchantMemberTypeService;
import com.zbkj.crmeb.member.vo.MerchantMemberStyleVO;
import com.zbkj.crmeb.member.vo.MerchantMemberTypeVO;
import com.zbkj.crmeb.user.model.UserMemberRecord;
import com.zbkj.crmeb.user.service.UserMemberRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商户会员类型表 服务实现类
 * </p>
 *
 * @author xx
 * @since 2022-05-10
 */
@Service
public class MerchantMemberTypeServiceImpl extends ServiceImpl<MerchantMemberTypeDao, MerchantMemberType> implements IMerchantMemberTypeService {
    @Autowired
    private IChannelMerchantService channelMerchantService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMemberRecordService userMemberRecordService;
    @Autowired
    private IChannelMerchantAttributeService channelMerchantAttributeService;

    @Override
    public CommonPage<MerchantMemberType> getMemberType(Integer merId, PageParamRequest pageParamRequest) {
        Page<MerchantMemberType> page = PageHelper.startPage(pageParamRequest.getPage(),pageParamRequest.getLimit());
        List<MerchantMemberType> list = this.list(Wrappers.lambdaQuery(MerchantMemberType.class)
                .eq(MerchantMemberType::getMerId, merId)
                .eq(MerchantMemberType::getDeleteFlag, false)
                .orderByDesc(MerchantMemberType::getSort));
        return CommonPage.restPage(CommonPage.copyPageInfo(page, list));
    }

    @Override
    public Boolean updMemberType(MerchantMemberTypeVO merchantMemberTypeVO) {
        MerchantMemberType merchantMemberType = new MerchantMemberType();
        BeanUtil.copyProperties(merchantMemberTypeVO,merchantMemberType);
        return this.updateById(merchantMemberType);
    }


    @Override
    public Boolean updMembershipAgreement(Integer merId, String membershipAgreement) {
        ChannelMerchantAttribute channelMerchantAttribute = channelMerchantAttributeService.getOne(Wrappers.lambdaQuery(ChannelMerchantAttribute.class).eq(ChannelMerchantAttribute::getMerId,merId));
        if (Objects.isNull(channelMerchantAttribute)){
            return false;
        }
        channelMerchantAttribute.setMembershipAgreement(membershipAgreement);
        return channelMerchantAttributeService.updateById(channelMerchantAttribute);
    }

    @Override
    public String getMembershipAgreement(Integer merId) {
        ChannelMerchantAttribute channelMerchantAttribute = channelMerchantAttributeService.getOne(Wrappers.lambdaQuery(ChannelMerchantAttribute.class).eq(ChannelMerchantAttribute::getMerId,merId));

        if (Objects.isNull(channelMerchantAttribute)){
            return null;
        }
        return channelMerchantAttribute.getMembershipAgreement();
    }

    @Override
    public Boolean updMemberStyle(Integer merId, MerchantMemberStyleVO merchantMemberStyleVO) {
        ChannelMerchant channelMerchant = channelMerchantService.getById(merId);
        if (Objects.isNull(channelMerchant)){
            return false;
        }
        channelMerchant.setBackgroundImageUrl(merchantMemberStyleVO.getBackgroundImageUrl());
        channelMerchant.setBackgroundColor(merchantMemberStyleVO.getBackgroundColor());
        channelMerchant.setDisplayStyle(merchantMemberStyleVO.getDisplayStyle());
        channelMerchant.setPersonalCenterFlag(merchantMemberStyleVO.getPersonalCenterFlag());
        channelMerchant.setProductDetailsFlag(merchantMemberStyleVO.getProductDetailsFlag());
        redisUtil.set(Constants.CHANNEL_MERCHANT + channelMerchant.getId(),channelMerchant,24L, TimeUnit.HOURS);
        return channelMerchantService.updateById(channelMerchant);
    }

    @Override
    public MerchantMemberStyleVO getMemberStyle(Integer merId) {
        ChannelMerchant channelMerchant = channelMerchantService.getById(merId);
        if (Objects.isNull(channelMerchant)){
            return null;
        }
        MerchantMemberStyleVO merchantMemberStyleVO = new MerchantMemberStyleVO();
        BeanUtil.copyProperties(channelMerchant,merchantMemberStyleVO);
        return merchantMemberStyleVO;
    }

    @Override
    public Boolean getMemberByMerId(Integer merId) {
        List<UserMemberRecord> userMemberRecordList = userMemberRecordService.list(Wrappers.lambdaQuery(UserMemberRecord.class)
                .eq(UserMemberRecord::getPaid,true)
                .eq(UserMemberRecord::getMerId,merId));
        return CollectionUtils.isNotEmpty(userMemberRecordList);
    }
}
